package org.soc.timesheet.domain;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.util.Collection;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table(name = "USERS_PROJECT")
@Configurable
@RooJavaBean
@RooJpaActiveRecord(versionField = "", table = "USERS_PROJECT")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "idSubProject", "idProject", "idUser" })
@RooJson
public class UsersProject {

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Id
    @GeneratedValue(generator = "alphanumeric")
    @GenericGenerator(name = "alphanumeric", strategy = "org.soc.timesheet.custom.domain.generators.RandomStringIdGenerator", parameters = { @org.hibernate.annotations.Parameter(name = "length", value = "100") })
    @Column(name = "ID", length = 100)
    private String id;

	@PersistenceContext
    transient EntityManager entityManager;

	public static final List<String> fieldNames4OrderClauseFilter = java.util.Arrays.asList("id");

	public static final EntityManager entityManager() {
        EntityManager em = new UsersProject().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countUsersProjects() {
        return entityManager().createQuery("SELECT COUNT(o) FROM UsersProject o", Long.class).getSingleResult();
    }

	public static List<UsersProject> findAllUsersProjects() {
        return entityManager().createQuery("SELECT o FROM UsersProject o", UsersProject.class).getResultList();
    }

	public static List<UsersProject> findAllUsersProjects(String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM UsersProject o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, UsersProject.class).getResultList();
    }

	public static UsersProject findUsersProject(String id) {
        if (id == null || id.length() == 0) return null;
        return entityManager().find(UsersProject.class, id);
    }

	public static List<UsersProject> findUsersProjectEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM UsersProject o", UsersProject.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	public static List<UsersProject> findUsersProjectEntries(int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        String jpaQuery = "SELECT o FROM UsersProject o";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        return entityManager().createQuery(jpaQuery, UsersProject.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            UsersProject attached = UsersProject.findUsersProject(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public UsersProject merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        UsersProject merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String toString() {
        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames("idSubProject", "idProject", "idUser").toString();
    }

	public String toJson() {
        return new JSONSerializer()
        .exclude("*.class").serialize(this);
    }

	public String toJson(String[] fields) {
        return new JSONSerializer()
        .include(fields).exclude("*.class").serialize(this);
    }

	public static UsersProject fromJsonToUsersProject(String json) {
        return new JSONDeserializer<UsersProject>()
        .use(null, UsersProject.class).deserialize(json);
    }

	public static String toJsonArray(Collection<UsersProject> collection) {
        return new JSONSerializer()
        .exclude("*.class").serialize(collection);
    }

	public static String toJsonArray(Collection<UsersProject> collection, String[] fields) {
        return new JSONSerializer()
        .include(fields).exclude("*.class").serialize(collection);
    }

	public static Collection<UsersProject> fromJsonArrayToUsersProjects(String json) {
        return new JSONDeserializer<List<UsersProject>>()
        .use("values", UsersProject.class).deserialize(json);
    }

	@ManyToOne
    @JoinColumn(name = "ID_SUB_PROJECT", referencedColumnName = "ID", nullable = false)
    private SubProjects idSubProject;

	@ManyToOne
    @JoinColumn(name = "ID_PROJECT", referencedColumnName = "ID", nullable = false)
    private Projects idProject;

	@ManyToOne
    @JoinColumn(name = "ID_USER", referencedColumnName = "ID", nullable = false)
    private Users idUser;

	public SubProjects getIdSubProject() {
        return idSubProject;
    }

	public void setIdSubProject(SubProjects idSubProject) {
        this.idSubProject = idSubProject;
    }

	public Projects getIdProject() {
        return idProject;
    }

	public void setIdProject(Projects idProject) {
        this.idProject = idProject;
    }

	public Users getIdUser() {
        return idUser;
    }

	public void setIdUser(Users idUser) {
        this.idUser = idUser;
    }
}
